<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="户型">
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入户型"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <div>
        <a-button type="primary" icon="plus" @click="$refs.addForm.add()">添加戶型</a-button>
      </div>
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <span slot="bedroomsscopedSlots" slot-scope="text">
          {{ 'room_type_bedrooms' | dictType(text) }}
        </span>
        <span slot="livingroomscopedSlots" slot-scope="text">
          {{ 'room_type_livingroom' | dictType(text) }}
        </span>
        <span slot="bathroomscopedSlots" slot-scope="text">
          {{ 'room_type_bathroom' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a  @click="$refs.editForm.edit(record)">編輯</a>
          <a-divider type="vertical"/>
          <a-popconfirm  placement="topRight" title="確認刪除？" @confirm="() => basicInformationDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { basicInformationPage, basicInformationDelete } from '@/api/modular/main/basicinformation/basicInformationManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm
    },
    data () {
      return {
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '戶型',
            align: 'left',
            dataIndex: 'name'
          },
          {
            title: '操作',
            width: '150px',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return basicInformationPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {
    },
    methods: {
      basicInformationDelete (record) {
        basicInformationDelete(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('刪除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
